﻿using Portfolio.Data;
using Portfolio.DataModels;
using Portfolio.Web.IDataAdapters.Interfaces;
using Portfolio.Web.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Data.Entity;

namespace Portfolio.Web.IDataAdapters.Adapters
{
    public class ProjectAdapter : IProjectAdapter
    {
        public List<DataModels.Project> GetAllProjects()
        {
            List<Project> projects;
            List<Project> newProjects = new List<Project>();
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                projects = db.Projects.OrderBy(x => x.Title).ToList();
            
                Project p;
                foreach(var x in projects){
                    p = new Project(x);
                    p.ImageUrls = db.ProjectImages.Where(z => z.ProjectId == x.Id).ToList();
                    newProjects.Add(p);
                }
            }
            return newProjects;
        }

        public DataModels.Project GetProject(int id)
        {
            Project project;
            Project p;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                project = db.Projects.Where(x => x.Id == id).FirstOrDefault();
                    p = new Project(project);
                    p.ImageUrls = db.ProjectImages.Where(z => z.ProjectId == id).ToList();
            }
            return p;
        }

        public DataModels.Project AddProject(DataModels.Project project)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                project = db.Projects.Add(project);
                foreach(var image in project.ImageUrls){
                    image.ProjectId = project.Id;
                    db.ProjectImages.Add(image);
                }
                db.SaveChanges();
            }
            return project;
        }

        public DataModels.Project UpdateProject(DataModels.Project project)
        {
            Project updatingProject;
            Project p;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                updatingProject = db.Projects.Where(x => x.Id == project.Id).FirstOrDefault();
                updatingProject.Title = project.Title;
                updatingProject.Description = project.Description;
                updatingProject.CodePlexUrl = project.CodePlexUrl;
                updatingProject.WindowsAzureUrl = project.WindowsAzureUrl;
                foreach (var i in project.ImageUrls)
                {
                    ProjectImage pi = db.ProjectImages.Where(x => x.Id == i.Id).FirstOrDefault();
                    if (pi==null)
                    {
                        i.ProjectId = project.Id;
                        db.ProjectImages.Add(i);
                    }
                    else
                    {
                        pi.Url = i.Url;
                    }
                }
                //updatingProject.ImageUrls = project.ImageUrls;
                db.SaveChanges();
            p = new Project(updatingProject);
            p.ImageUrls = db.ProjectImages.Where(z => z.ProjectId == updatingProject.Id).ToList();
            }
            return p;
        }

        public void DeleteProject(int id)
        {
            Project project;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                //delete images associated to project
                List<ProjectImage> list = db.ProjectImages.Where(x => x.ProjectId == id).ToList();
                foreach(var img in list){
                    db.ProjectImages.Remove(img);
                }
                //delete project
                project = db.Projects.Where(x => x.Id == id).FirstOrDefault();
                db.Projects.Remove(project);
                db.SaveChanges();
            }
        }

        public void SendEmail(string address, string subject, string message)
        {
            string email = ConfigurationManager.AppSettings.Get("email");
            string password = ConfigurationManager.AppSettings.Get("password");
            string client = ConfigurationManager.AppSettings.Get("client");
            string port = ConfigurationManager.AppSettings.Get("port");

            NetworkCredential loginInfo = new NetworkCredential(email, password);
            MailMessage msg = new MailMessage();
            SmtpClient smtpClient = new SmtpClient(client, int.Parse(port));

            msg.From = new MailAddress(email);
            msg.To.Add(new MailAddress(address));
            msg.Subject = subject;
            msg.Body = message;
            msg.IsBodyHtml = true;

            smtpClient.EnableSsl = true;
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = loginInfo;
            smtpClient.Send(msg);
        }

    }
}